Part Number Hot Search : 
MC3843 PITE1 ST75C M2049TNG DDC142JH B2566 SKIIP30 NT2955
Product Description
Full Text Search
 

To Download AN1530 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  AN1530/0702 1/12 AN1530 application note accurate timebase for low-cost st7 applications with internal rc oscillator by microcontroller division applications introduction the st7 microcontroller contains an internal rc oscillator, which may vary due to internal component variation depending upon the surrounding conditions. this can lead to the wrong calculation of the timing for the different peripherals like timers, sci etc. timing calculation for the peripheral can be done based on the internal rc frequency given in the datasheet, but due to the variations in internal rc frequency from component to component, this leads to wrong timing results in the application. the purpose of this application note is to present a software solution for accurate timing by cal- ibrating the internal peripheral parameters against the variation of the internal rc oscillator. this note focuses on st7 mcus with a non-calibrated rc. a 50 hz, 5 v source is applied as a reference frequency to find the real internal rc oscillator frequency. the deviation of the in- ternal frequency with respect to the datasheet frequency is applied to correct the peripheral parameter values to obtain timing accuracy. this solution provides an innovative way to com- pensate the internal rc oscillator variation and to develop low cost applications. software is developed with using the st7 software library (available free on the st web site). software shows, how to calibrate pwm signal of timer a to produce a 5 khz frequency. it also gives an idea of how to calibrate the baud rate of the sci (here it is calibrated for 9600 baud). an externally calibrated source (ex. function generator) is used to apply 50 hz and 5 v as a reference. 1
2/12 accurate timebase for low-cost st7 applications with internal rc... 1 software solution 1.1 measurement principle to measure the frequency of the internal rc, software uses the input capture pin of 16-bit timer a. a calibrated source is applied with 50hz on the capture pin. every falling edge on this pin generates an interrupt (at 20ms time duration). software has taken the internal rc frequency of typically 4 mhz as per the datasheet. the timer clock is selected as fcpu/2 (1 mhz). counter overflow is generated after 65.532 ms (0xffff). this internal rc frequency should be chosen between the datasheet. max. and min. rc frequency values. capture1 and capture2 are stored as shown in figure 1. figure 1. timer input capture to measure the reference frequency. here two possibilities can occur during the calculation of the capture elapsed time. if capture1 and capture2 are on the single counter (as shown in figure 1), time is calculated simply by capture counter = capture2 - capture1 second possibility is that counter overflow generates between the measurement of the capture1 and capture2. this leads to a more complex calculation given by capture counter= ((0xffff - capture1) + capture2) this measured capture counter is compared with the ideal capture counter. ideal capture counter is calculated manually and here for the fcpu = 2 mhz, it gives 0x4e20. with this infor- mation internal rc deviation is found. ffffh 0000h calibrated source for 5 vpp, 50 hz free running counter of timer a capture1 capture2 2
3/12 accurate timebase for low-cost st7 applications with internal rc... this fraction can be used to calibrate the other peripheral parameters to add precision to the the timing calculations. if a mains supply is used as an external source, this solution works for an european supply (50 hz)). if a us supply (60 hz) is used, the calculated capture counter will be 0x411b to find the fraction for internal rc deviation. a software routine is developed to find this fraction and utilize it to calibrate the peripheral pa- rameters. this routine is included in the application. the basic software takes less than 30ms to find the fraction, which gives the deviation of real rc frequency with reference to the da- tasheet frequency. this application includes a glitch filter algorithm and also takes the average of 8 samples for finding the fraction. the basic algorithm and the average + glitch algorithms are described in the next chapters. the software takes less than 325ms with the averaging and glitch algorithms. this routine execution time includes the 8 capture elapsed times, waiting times for the cap- tures and the loop calculations inside the routine. the routine execution time is affected mostly by the capture waiting times. this routine will run in the background of the main application, so mcu can run other tasks during this 30ms (basic software algorithm) or 325ms (for average and glitch algorithm) period. internalrcdeviationfraction measuredcapturecounter calculatedcapturecounter ------------------------------------------------------------------------------ - =
4/12 accurate timebase for low-cost st7 applications with internal rc... 1.2 basic algorithm this algorithm measures the deviation of internal rc oscillator frequency in terms of fraction. this will take 30ms to find the fraction. software is developed in c language with the help of the st7 software library. it needs 9 bytes of ram. the software works as shown in the following flow chart for a single sample. figure 2. basic algorithm flowchart start initialisation of timer a capture measurement of the capture1 and capture2 done is counter overflow generated ? capture counter= ( ( 0xffff - capture1 ) + capture2 ) yes capture counter= capture2 - capture1 no internal rc deviation fraction = measured capture counter / calculated (ideal) capture counter
5/12 accurate timebase for low-cost st7 applications with internal rc... 1.3 averaging and glitch algorithm this algorithm an added feature to section1.2. when the 50 hz source has noise or spikes, this algorithm helps to prevent a wrong measurement of the reference signal. if you develop a permanent hardware circuit to generate the 50 hz signal, then it may contain noise signals as well as glitches or spikes. this algorithm is a must in this case. this software uses 12 bytes of ram and takes 325 ms (one loop to find the average of 8 sam- ples) to find the deviation fraction. the flowchart shows how this algorithm works. figure 3. average and glitch algorithm flowchart yes ? capture_range sample = (+/-)10% of first sample of capture counter is stored (capture_range) for glitch filtering initialisation of timer a capture and variables check no average internal rc deviation fraction = average capture counter / calculated (ideal) capture counter yes ? over 8 samples is no (figure 2) start capture counter measurement
6/12 accurate timebase for low-cost st7 applications with internal rc... 1.4 calibration of peripheral parameters 1.4.1 pwm counter software calibrates the pwm of timer to produce the pulse train at 5 khz frequency. normal pwm counter is given by the equation, where, pwmcounter = counter value to load in ocir register t = pulse period (in seconds) fcpu = cpu clock frequency presc = prescaler selection variation in the internal rc affects the fcpu in above equation. the measured fraction (section 1.3) is applied to this term of the equation. this leads above equation to the new version, calibration software calculates pwm counter value using the second equation. it takes care of the internal rc variation and loads the correct value in the ocir register to produce the de- sired frequency. here for the 5 khz frequency generation, pwmcounter will be 95 for fcpu = 2 mhz. when you convert into hex value from decimal, resolution will be of 1 count. so the accuracy is [(1/ 95)*100] = 1.05%. the accuracy will be proportional to the variation in t and same as the counter. 1.4.2 sci baud rate sci applications are the real time-critical applications. the transmit baud rate can be deter- mined with the following equation, where, tx = baud rate for transmission fcpu = cpu clock frequency pr = sci generic prescaler pwmcounter tfcpu presc --------------------- -5 C ? ?? = pwmcounter t fcpu fraction presc ------------------------------------------------ 5 C ? ?? = tx fcpu 16 pr () tr etpr --------------------------------------------------------------- =
7/12 accurate timebase for low-cost st7 applications with internal rc... tr =transmission rate divisor etpr = extended transmit prescaler division the above equation is used normally without the etpr selection. due to the flexibility of etpr range (1 to 255), this software solution calibrates the sci baud rate using the etpr register. software keeps pr=1 and tr=1. again the fraction is applied to the numerator of the equation as the variation affects the fcpu only. the equation becomes, to calibrate the baud rate, software loads the value of 9600 in tx. this gives the value in terms of (pr * tr * etpr). software loads tr=pr=1, so the value is directly assigned to the etpr register. this value is loaded into the pr, tr and etpr bits. for the 9600 baud rate, this calibration gives ~9300 baud rate (where internal rc varies (-25%)). this software is able to communicate with the pc hyperterminal after using this calibration. here for the 9600 baud rate calculation, etpr = 13, pr = tr = 1, for fcpu = 2 mhz. when you convert into hex value from decimal, resolution will be of 0.5 count. so the accuracy is [(0.5/ 13)*100] = 3.84%. accuracy will be proportional to variations in baud rate and same as that of the etpr. for higher baud rate values, you have to go for a resolution 0.25 (logic can be ex- tended as shown in software) to achieve better accuracy. if sci doesnt have the etpr prescaler then we can load pr=1 and assign the counted value to tr directly. 2 conclusion this software solution allows you to work with any time critical applications irrespective of the variations of the internal rc oscillator frequency. software is calibrated with reference to the base of 4 mhz. software is developed using the st7 software library functions. it takes less than 30ms for the simple calibration version. with the averaging and glitch algorithms, it takes less than 325 ms. software includes correction for the pwm counter value and the sci baud rate. the pwm counter value gives the frequency with an accuracy of 1-2% and the sci baud rate gives an accuracy of 3-4% after using this calibration software, when internal rc oscillator varies by (- 25%). tx fcpu fraction 16 pr () tr etpr --------------------------------------------------------------- =
8/12 accurate timebase for low-cost st7 applications with internal rc... 3 software example this software example includes all the algorithms. it includes a basic algorithm for finding the fraction and also contains average and glitch algorithms. it has calibration routines for the pwm counter and sci baud rate. 3.1 main program /******************************************************************************** copyright 2002 stmicroelectronics source file name: main.c group : ipsw, cmg - ipdf author : mcd application team date first issued: 01/07/2002 ********************************documentation********************************** general purpose - this routine provides internal rc deviation measurement and calibration of pwm counter and sci baud rate. ********************************revision history******************************** date: 01/07/2002 release: 1.0 ******************************************************************************/ #include "st7lib_config.h" /*configuration file*/ unsigned char cap; unsigned int capture1,capture2; const unsigned char buff1[36]= " sci calibration done "; /****************************************************************************** main application begins ******************************************************************************/ #define timerb_pwm_cal #define sci_cal void main (void) { /****************************************************************************** variable declaration for measuring the capture inputs with averaging and glitch filter algorithm ******************************************************************************/ unsigned char i; unsigned int capture_range; float fraction=0.0; /****************************************************************************** variable declaration for calibrating pwm w.r.t internal rc frequency ******************************************************************************/ unsigned int pwm_cnt, pwm_cnt1; /****************************************************************************** variable declaration to calibrate sci baudrate w.r.t internal rc frequency ******************************************************************************/ unsigned char caltr; float caltr_temp = 0.0; /****************************************************************************** initialisation of the varibles ******************************************************************************/ cap=capture1=capture2=0; pwm_cnt = 0; pwm_cnt1 = 0; caltr =0; capture_range = 0;
9/12 accurate timebase for low-cost st7 applications with internal rc... /****************************************************************************** enable mcc to watch the real internal rc oscillator frequency ******************************************************************************/ mcc_init(mcc_default); mcc_init(mco_enable); /****************************************************************************** timer a library called to capture the rising edge at the pin pf4 ******************************************************************************/ timera_init(timer_fcpu_2); /*timer clock is fcpu/2=1 mhz at fosc=4mhz*/ timera_icap_mode(timer_icap_1,timer_edge_0); timera_it_enable(timer_icap_it_enable); enableinterrupts; /****************************************************************************** loop to capture 8 samples and to do averaging of it with using glitch filter algorithm to protect the wrong signal capture ******************************************************************************/ for (i=0;i<8;i++) /*averaging done for 8 samples*/ { while (cap != 2); /*wait loop for two captures*/ /*management the possibility of overflow of counter*/ if (capture2 < capture1) { capture2 = ((0xffff - capture1) + capture2); } else { capture2 = (capture2 - capture1); } capture2 = capture2 >> 3; /**********************glitch filtering algorithm **********************/ if (i==0) { capture_range = capture2; } fraction = fraction + capture2; if((capture2>(1.1*capture_range))||(capture2<(0.9 * capture_range))) { fraction =0; i=0; } capture1=capture2=cap=0; } /*fraction = measured rc frequency/assumed internal rc frequency (here 4 mhz)*/ fraction = ((float)fraction/0x4e20); nop; /****************************************************************************** timer parameter calibration ******************************************************************************/ #ifdef timerb_pwm_cal /********** timer pwm generation for 5 khz frequency*****************/ /****************************************************************************** 100 value to get 100 microcesconds at 2mhz fcpu, equation is used as per st72f521 datasheet ******************************************************************************/ pwm_cnt = (int)(200 * fraction); /*numerator calculation*/
10/12 accurate timebase for low-cost st7 applications with internal rc... pwm_cnt = pwm_cnt >> 1; /*division by 2*/ pwm_cnt = pwm_cnt - 5; pwm_cnt1 = (int)(400 * fraction); /*numerator calculation*/ pwm_cnt1 = pwm_cnt1 >> 1; /*division by 2*/ pwm_cnt1 = pwm_cnt1 - 5; timerb_init(timer_fcpu_2); timerb_pwm_mode(timer_output1_r, timer_output2_f, pwm_cnt, pwm_cnt1); nop; #endif /****************************************************************************** sci transmit time calibration ******************************************************************************/ #ifdef sci_cal #ifdef sci_polling_tx /****************************************************************************** sci transmit time calibration for 9600 baudrate at fcpu=2mhz, this communication is done with hyperterminal of pc with the configuration: baudrate=9600,databits-8, stop bit-1,parity-none and flowcontrol-none ******************************************************************************/ caltr_temp = (float) (13 * fraction); /***********value 13 = pr*tr*etpr, where pr=tr=1. etpr = 13*******************/ caltr = (char)(13 * fraction); caltr_temp = (float)(caltr_temp - caltr); if (caltr_temp > 0.5) { caltr = caltr + 1; } /***********sci library called to send the message on hyperterminal***********/ sci_init(sci_default_param1, sci_default_param2); sci_extend_baudrate(sci_pr_1 + sci_tr_1 + sci_rr_1, caltr, caltr); sci_mode(sci_tx_enable); sci_putbuffer(buff1,sizeof (buff1)); sci_putbyte(0x55); while(!(sci_istransmitcompleted())); #endif #endif } /****************************************************************************** end of main ******************************************************************************/ /****************************************************************************** interrupt subroutine for timer ******************************************************************************/ #ifdef _hiware_ /* test for hiware compiler */ #pragma trap_proc save_regs /* additional registers will be saved */ #else #ifdef _cosmic_ /* test for cosmic compiler */ @interrupt /* cosmic interrupt handling */ #else#error"unsupported compiler!" /* compiler defines not found!*/ #endif #endif void timera_it_routine () { if (!cap) {
11/12 accurate timebase for low-cost st7 applications with internal rc... while (timera_status_flag(timer_flag_icf1)!= true); cap++; capture1 = timera_icap_getvalue(timer_icap_1); timera_clear_flag(timer_flag_icf1); } else if (cap) { while (timera_status_flag(timer_flag_icf1)!= true); capture2 = timera_icap_getvalue(timer_icap_1); timera_clear_flag(timer_flag_icf1); cap++; } } /**** (c) 2002 stmicroelectronics *************************** end of file **/
12/12 accurate timebase for low-cost st7 applications with internal rc... the present note which is for guidance only aims at providing customers with information regarding their products in order for them to save time. as a result, stmicroelectronics shall not be held liable for any direct, indirect or consequential damages with respect to any claims arising from the content of such a note and/or the use made by customers of the information contained herein in connection with their products. information furnished is believed to be accurate and reliable. however, stmicroelectronics assumes no responsibility for the co nsequences of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. no license is granted by implication or otherwise under any patent or patent rights of stmicroelectronics. specifications mentioned in this publicati on are subject to change without notice. this publication supersedes and replaces all information previously supplied. stmicroelectronics prod ucts are not authorized for use as critical components in life support devices or systems without the express written approval of stmicroele ctronics. the st logo is a registered trademark of stmicroelectronics ? 2002 stmicroelectronics - all rights reserved. purchase of i 2 c components by stmicroelectronics conveys a license under the philips i 2 c patent. rights to use these components in an i 2 c system is granted provided that the system conforms to the i 2 c standard specification as defined by philips. stmicroelectronics group of companies australia - brazil - canada - china - finland - france - germany - hong kong - india - israel - italy - japan malaysia - malta - morocco - singapore - spain - sweden - switzerland - united kingdom - u.s.a. http://www.st.com


▲Up To Search▲   

 
Price & Availability of AN1530

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X